﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using WebManager;
namespace BaoJian.WebManager.Function.Class
{
    public partial class NewClass : System.Web.UI.Page
    {
        private string ColumnID = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string sql = "SELECT DISTINCT [MainCatalogID],[MainCatalogName]  FROM [Class]";
            MainCatalogNameList.DataSource = Maticsoft.DBUtility.DbHelperSQL.Query(sql).Tables[0];
            MainCatalogNameList.DataBind();
            ListItem item = new ListItem("请选择", "");
            MainCatalogNameList.Items.Insert(0, item);
        }

        protected void Ret_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/Function/Class/List.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }

        protected void Save1_Click(object sender, EventArgs e)
        {

            Label label = Page.Master.FindControl("lblTitle") as Label;
            if (string.IsNullOrEmpty(MainCatalogNameList.SelectedValue) && string.IsNullOrEmpty(MainCatalogName.Text))
            {
                label.Text = "请选择或者输入主分类";
                return;
            }

            bool exist = false;
            string sql = "SELECT COUNT(*) AS Num FROM [Class] WHERE [MainCatalogName]='{0}'";
            sql = string.Format(sql, MainCatalogName.Text.Trim());
            SqlDataReader reader = Maticsoft.DBUtility.DbHelperSQL.ExecuteReader(sql);
            if (reader.Read())
            {
                if (Convert.ToInt32(reader["Num"] as string) > 0)
                {
                    exist = true;
                }
            }
            reader.Close();

            if (exist)
            {              
                label.Text = "该类型已经存在";
                return;
            }

            sql = "SELECT COUNT(*) AS Num FROM [Class] WHERE [MainCatalogName]='{0}'AND [SubCatalogName]='{1}'";

            if(!string.IsNullOrEmpty(MainCatalogNameList.SelectedValue))
                sql = string.Format(sql, MainCatalogNameList.SelectedItem.Text, SubCatalogName.Text.Trim());
            else
                sql = string.Format(sql, MainCatalogName.Text.Trim(), SubCatalogName.Text.Trim());
            reader = Maticsoft.DBUtility.DbHelperSQL.ExecuteReader(sql);
            if (reader.Read())
            {
                if (Convert.ToInt32(reader["Num"]) > 0)
                {
                    exist = true;
                }
            }
            reader.Close();

            if (exist)
            {               
                label.Text = "该子分类已经存在";
                return;
            }
            LYQ.BLL.Class bll = new LYQ.BLL.Class();
            LYQ.Model.Class model = new LYQ.Model.Class();

            if (!string.IsNullOrEmpty(MainCatalogNameList.SelectedValue))
            {
                model.MainCatalogID = MainCatalogNameList.SelectedValue;
                model.MainCatalogName = MainCatalogNameList.SelectedItem.Text;
            }
            else
            {
                model.MainCatalogName = MainCatalogName.Text;
                model.MainCatalogID = Guid.NewGuid().ToString();
            }

            model.SubCatalogID = Guid.NewGuid().ToString();
            model.SubCatalogName = SubCatalogName.Text;

            bll.Add(model);


            string url = URLUtility.UrlBase + "/WebManager/Function/Class/List.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }
    }
}
